<template>
    <Dialog v-model="dialogVisible" title="上课范围设定" width="600" :draggable="false">
        <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="90px">
            <el-form-item label="打卡范围" prop="radius">
                <el-badge value="单位: 米" type="primary">
                    <el-input-number v-model="formData.radius" :min="1" :max="999999"/>
                </el-badge>
            </el-form-item>
<!--            <el-form-item label="经纬度" prop="location">-->
<!--                <Map v-model:location="formData.location"/>-->
<!--            </el-form-item>-->
          <!-- 经度、纬度和按钮布局 -->
          <el-form-item label="位置" required>
            <el-row :gutter="10" style="align-items: center;">
              <el-col :span="8">
                <!--            <el-input v-model="formData.lng" placeholder="请输入经度"/>-->
                <el-form-item prop="lng">
                  <el-input v-model="formData.lng" placeholder="请输入经度"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <!--            <el-input v-model="formData.lat" placeholder="请输入纬度"/>-->
                <el-form-item prop="lat">
                  <el-input v-model="formData.lat" placeholder="请输入纬度"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button type="primary" @click="openMap">打开地图</el-button>
              </el-col>
            </el-row>
          </el-form-item>
            <el-form-item label="课程时长" prop="duration">
                <el-badge value="单位: 分钟" type="primary">
                    <el-input-number v-model="formData.duration" :min="1" :max="300"/>
                </el-badge>
            </el-form-item>
        </el-form>

        <template #footer>
            <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
        </template>
    </Dialog>
</template>
<script lang="ts" setup>
import {useConfirm, useMessage} from "@/components/Notice";
import {useCourseApi} from "@/api/yxt/course";

defineOptions({name: 'YxtCourseSignSetting'})

const courseApi = useCourseApi();

const {t} = useI18n()
const dialogVisible = ref(false)
const formLoading = ref(false)
const formData = ref({
    id: undefined,
    radius: 0,
    duration: 0,
    location: [],
    lng: '',
    lat: '',
})

const formRef = ref()
const formRules = reactive({
    radius: [{required: true, message: '打卡范围不能为空', trigger: ['blur', 'change']}],
    lng: [{required: true, message: '位置经度不能为空', trigger: ['blur', 'change']}],
    lat: [{required: true, message: '位置纬度不能为空', trigger: ['blur', 'change']}],
    duration: [{required: true, message: '课程时长不能为空', trigger: ['blur', 'change']}],
})

const open = async () => {
    dialogVisible.value = true;
    resetForm();
    formLoading.value = true;
    try {
        courseApi.getConfig().then((res) => {
            formData.value.radius = res.radius;
            formData.value.duration = res.duration;
            formData.value.lng = res.location[0];
            formData.value.lat = res.location[1];
        });
    } finally {
        formLoading.value = false
    }
}
defineExpose({open})

const emit = defineEmits(["success"])
const submitForm = () => {
    formRef.value?.validate(async (valid: any) => {
        if (valid) {
            useConfirm("确定更新上课打卡位置?", () => {
                formLoading.value = true
                try {
                  // 将经纬度存储到 location 数组中
                  formData.value.location = [formData.value.lng, formData.value.lat];
                  courseApi.updateConfig(formData.value);
                    useMessage(t('common.updateSuccess'));
                    dialogVisible.value = false
                    emit('success')
                } finally {
                    formLoading.value = false
                }
            })
        }
    });
}

const resetForm = () => {
    formData.value = {
        id: undefined,
        radius: 0,
        location: [],
    }
    formRef.value?.resetFields()
}
// 地图打开函数
const openMap = () => {
  window.open("https://lbs.amap.com/tools/picker/", "_blank");
};

</script>
